<?php

/**
 * Shipping information page
 *
 * Shopalong, the open source shopping system
 * http://shopalong.blogspot.com/
 *
 * @author Stuart Metcalfe <shoopdev@gmail.com>
 * @copyright Copyright &copy; 2003 osCommerce
 * @copyright Portions Copyright &copy; 2006, Shopalong
 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
 */

require_once('includes/application_top.php');

if (isset($HTTP_GET_VARS['view']) && $HTTP_GET_VARS['view'] == 'success')
{
	if (sizeof($navigation->snapshot) > 0) {
		$origin_href = tep_href_link($navigation->snapshot['page'], tep_array_to_string($navigation->snapshot['get'], array($session->getName())), $navigation->snapshot['mode']);
		$navigation->clear_snapshot();
	} else {
		$origin_href = tep_href_link(FILENAME_DEFAULT);
	}
	
	$theme->assign('origin_href', $origin_href);
	$theme->display('create_account_success.tpl');
	require_once(DIR_WS_INCLUDES.'application_bottom.php'); 
	exit(0);
}

$form = new FormObserver('submit',$messageStack,'create_account');
$optVal = new OptionValidator($form);
$accountEmailVal = new AccountEmailValidator($form);
$alphaVal = new AlphaValidator($form);
$stringVal = new StringValidator($form);
$intVal = new IntegerValidator($form);
$emailVal = new EmailValidator($form);
$dateVal = new DateValidator($form);
$matchVal = new MatchValidator($form);
$stateVal = new StateValidator($form);
$countryVal = new CountryValidator($form);

if ((boolean) ACCOUNT_GENDER)
{
	$optVal->validate('gender','ENTRY_GENDER_ERROR',array('m','f'));
}

$alphaVal->validate('firstname','ENTRY_FIRSTNAME_ERROR',ENTRY_FIRST_NAME_MIN_LENGTH);
$alphaVal->validate('lastname','ENTRY_LASTNAME_ERROR',ENTRY_LAST_NAME_MIN_LENGTH);

if ((boolean) ACCOUNT_DOB)
{
	$dateVal->validate('dob','ENTRY_DOB_ERROR');
}

$emailVal->validate('email_address','ENTRY_EMAIL_ERROR',ENTRY_EMAIL_ADDRESS_MIN_LENGTH);
$accountEmailVal->validate('email_address','ENTRY_EMAIL_ADDRESS_ERROR_EXISTS',false);
$stringVal->validate('street_address','ENTRY_STREET_ADDRESS_ERROR',ENTRY_STREET_ADDRESS_MIN_LENGTH);
$stringVal->validate('postcode','ENTRY_POSTCODE_ERROR',ENTRY_POSTCODE_MIN_LENGTH);
$alphaVal->validate('city','ENTRY_CITY_ERROR',ENTRY_CITY_MIN_LENGTH);

if ((boolean) ACCOUNT_STATE)
{
	$stateVal->validate('state','ENTRY_STATE_ERROR',$HTTP_POST_VARS['country'],ENTRY_STATE_MIN_LENGTH);
}

$countryVal->validate('country','ENTRY_COUNTRY_ERROR');
$stringVal->validate('telephone','ENTRY_TELEPHONE_ERROR',ENTRY_TELEPHONE_MIN_LENGTH);
$stringVal->validate('password','ENTRY_PASSWORD_ERROR',ENTRY_PASSWORD_MIN_LENGTH);
$matchVal->validate('password','ENTRY_PASSWORD_ERROR_NOT_MATCHING',$HTTP_POST_VARS['confirmation']);

// check for errors
if ($form->isValid())
{
	if ((boolean) ACCOUNT_GENDER)
	{
		if (isset($HTTP_POST_VARS['gender']))
		{
			$gender = tep_db_prepare_input($HTTP_POST_VARS['gender']);
		}
		
		else
		{
			$gender = false;
		}
	}
	
	$firstname = tep_db_prepare_input($HTTP_POST_VARS['firstname']);
	$lastname = tep_db_prepare_input($HTTP_POST_VARS['lastname']);
	
	if (ACCOUNT_DOB == 'true')
	{
		$dob = tep_db_prepare_input($HTTP_POST_VARS['dob']);
	}
	
	$email_address = tep_db_prepare_input($HTTP_POST_VARS['email_address']);
	
	if (ACCOUNT_COMPANY == 'true')
	{
		$company = tep_db_prepare_input($HTTP_POST_VARS['company']);
	}
	
	$street_address = tep_db_prepare_input($HTTP_POST_VARS['street_address']);
	
	if (ACCOUNT_SUBURB == 'true')
	{
		$suburb = tep_db_prepare_input($HTTP_POST_VARS['suburb']);
	}
	
	$postcode = tep_db_prepare_input($HTTP_POST_VARS['postcode']);
	$city = tep_db_prepare_input($HTTP_POST_VARS['city']);
	
	if (ACCOUNT_STATE == 'true')
	{
		$state = tep_db_prepare_input($HTTP_POST_VARS['state']);
		
		if (isset($HTTP_POST_VARS['zone_id']))
		{
			$zone_id = tep_db_prepare_input($HTTP_POST_VARS['zone_id']);
		}
		
		else
		{
			$zone_id = false;
		}
	}
	
	$country = tep_db_prepare_input($HTTP_POST_VARS['country']);
	$telephone = tep_db_prepare_input($HTTP_POST_VARS['telephone']);
	$fax = tep_db_prepare_input($HTTP_POST_VARS['fax']);
	$password = tep_db_prepare_input($HTTP_POST_VARS['password']);
	$confirmation = tep_db_prepare_input($HTTP_POST_VARS['confirmation']);
	$sql_data_array = array(
		'customers_firstname' => $firstname,
		'customers_lastname' => $lastname,
		'customers_email_address' => $email_address,
		'customers_telephone' => $telephone,
		'customers_fax' => $fax,
		'customers_newsletter' => $newsletter,
		'customers_password' => tep_encrypt_password($password)
	);
	
	if (ACCOUNT_GENDER == 'true')
	{
		$sql_data_array['customers_gender'] = $gender;
	}
	
	if (ACCOUNT_DOB == 'true')
	{
		$sql_data_array['customers_dob'] = tep_date_raw($dob);
	}
	
	tep_db_perform(TABLE_CUSTOMERS, $sql_data_array);
	$customer_id = tep_db_insert_id();
	$sql_data_array = array(
		'customers_id' => $customer_id,
		'entry_firstname' => $firstname,
		'entry_lastname' => $lastname,
		'entry_street_address' => $street_address,
		'entry_postcode' => $postcode,
		'entry_city' => $city,
		'entry_country_id' => $country
	);
	
	if (ACCOUNT_GENDER == 'true')
	{
		$sql_data_array['entry_gender'] = $gender;
	}
	
	if (ACCOUNT_COMPANY == 'true')
	{
		$sql_data_array['entry_company'] = $company;
	}
	
	if (ACCOUNT_SUBURB == 'true')
	{
		$sql_data_array['entry_suburb'] = $suburb;
	}
	
	if (ACCOUNT_STATE == 'true')
	{
		if ($zone_id > 0)
		{
			$sql_data_array['entry_zone_id'] = $zone_id;
			$sql_data_array['entry_state'] = '';
		}
		
		else
		{
			$sql_data_array['entry_zone_id'] = '0';
			$sql_data_array['entry_state'] = $state;
		}
	}
	
	tep_db_perform(TABLE_ADDRESS_BOOK, $sql_data_array);
	$address_id = tep_db_insert_id();
	tep_db_query('update '.TABLE_CUSTOMERS.' set customers_default_address_id = "'.(int)$address_id.'" where customers_id = "'.(int)$customer_id.'"');
	tep_db_query('insert into '.TABLE_CUSTOMERS_INFO.' (customers_info_id, customers_info_number_of_logons, customers_info_date_account_created) values ("'.(int)$customer_id.'", "0", now())');
	
	// Notify listeners that the form was successully submitted
	$eventManager->fireEvent('FormSubmitted', array('formName'=>'create_account'));
	
	if (SESSION_RECREATE == 'True')
	{
		$session->recreate();
	}
	
	$customer_first_name = $firstname;
	$customer_default_address_id = $address_id;
	$customer_country_id = $country;
	$customer_zone_id = $zone_id;
	$session->register('customer_id');
	$session->register('customer_first_name');
	$session->register('customer_default_address_id');
	$session->register('customer_country_id');
	$session->register('customer_zone_id');
	
	// restore cart contents
	$cart->restoreContents();
	
	// Send a confirmation email
	$theme->assign('gender', $gender);
	$theme->assign('lastname', $lastname);
	$theme->assign('firstname', $firstname);
	$emailText = $theme->fetch('create_account_success_email.tpl');
	tep_mail(
		$name,
		$email_address,
		sprintf($theme->get_config_vars('EMAIL_SUBJECT'), STORE_NAME),
		$emailText,
		STORE_OWNER,
		STORE_OWNER_EMAIL_ADDRESS
	);
	
	tep_redirect(tep_href_link(FILENAME_CREATE_ACCOUNT.'?view=success', '', 'SSL'));
}

else
{
	// Map form errors to message stack
	if ($form->isSubmitted())
	{
		foreach ($form->getErrors() as $error)
		{
			$messageStack->add('create_account', $error);
		}
	}
	
	$theme->assign('zones', sal_getZones($HTTP_POST_VARS['country']));
	$theme->assign('countries', tep_get_countries());
	$theme->assign_by_ref('form', $form);
	$theme->display('create_account.tpl');
}

require_once(DIR_WS_INCLUDES.'application_bottom.php'); 

?>
